home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 80 / CD Actual 80 Julio-Agosto 2003.iso / Linux / LinuxGazette / lg / issue76 / misc / rogers / rogers_example07.c
Encoding:
C/C++ Source or Header  |  2002-08-14  |  9.1 KB  |  347 lines

  1. #include <string.h>
  2. #include <stdio.h>
  3. /*
  4.  *
  5.  *     This program is written to demonstrate the <string.h> library.
  6.  *
  7.  *     Written by James M. Rogers
  8.  *
  9.  *     02 Feb 2002
  10.  *
  11.  *     Released to the Public Domain on this date.
  12.  *
  13.  *
  14.  */
  15.  
  16.  
  17.  
  18.  
  19. #define  MAX_STRING_LENGTH  17
  20. #define STATIC_STRING "This is a long string that will be copied into a location during runtime"
  21.  
  22. main (){
  23.  
  24.     char  aa[10];
  25.     char  ab[100];
  26.     char  ac[1000];
  27.     char  ad[10000];
  28.  
  29.     char *ba;
  30.     char *bb;
  31.     char *bc;
  32.     char *bd;
  33.     char *be;
  34.  
  35.     char string_a[17];
  36.     char string_b[MAX_STRING_LENGTH];
  37.     char *string_c;
  38.     int string_length;
  39.  
  40.     int    x;
  41.     size_t y;
  42.  
  43. /* Examples */
  44.  
  45.     /* This reserves room for 16 characters and an end of string marker. */
  46.  
  47.  
  48.     strcpy ( string_a, "This is a string" );
  49.     printf("\n strcpy ( string_a, \"This is a string\" ); \n string_a = %s \n", string_a);
  50.  
  51.   /* the following copies a string in that is too large. */
  52.   /* it shouldn't work, but often does.  */
  53.     strcpy ( string_a, "This is a long string" );
  54.     printf("\n strcpy ( string_a, \"This is a string\" ); \n string_a = %s \n", string_a);
  55.  
  56.  
  57.     strncpy ( string_b, "This is a long string", MAX_STRING_LENGTH );
  58.     string_b[MAX_STRING_LENGTH-1] = '\000';
  59.     printf("\n strcpy ( string_b, \"This is a string\" ); \n string_b = %s \n", string_b);
  60.     
  61.  
  62.  
  63.     string_length = strlen(STATIC_STRING);
  64.     
  65.     if (!(string_c = (char *) malloc ( string_length ))){
  66.        /* no memory left, die */
  67.        exit (1);
  68.     }
  69.  
  70.     strncpy( string_c,  STATIC_STRING, string_length);
  71.     string_c[string_length] = '\000';
  72.     printf("\n string_c = %s \n", string_c);
  73.  
  74.     /* do something with the string */
  75.     free(string_c);
  76.  
  77.  
  78. /* Copy */
  79.  
  80.     printf ("\n* Copy \n");
  81.  
  82.   /* void  *memcpy(void *dest, const void *src, size_t n); */
  83.  
  84.     memcpy (aa, "test", 5);
  85.     printf ("\n memcpy (aa, \"test\", 5) \n aa = \"%s\"\n", aa);
  86.  
  87.   /* void *memmove(void *dest, const void *src, size_t n); */
  88.  
  89.     memmove (ab, aa, 10);
  90.     printf ("\n memmove (ab, aa, 10) \n ab = \"%s\"\n", ab);
  91.  
  92.   /* char *strncpy(char *dest, const char *src, size_t n); */
  93.  
  94.     strncpy (aa, "ghij", 5);
  95.     printf ("\n strncpy (aa, \"ghij\", 5) \n aa = \"%s\"\n", aa);
  96.  
  97.   /* char  *strcpy(char *dest, const char *src); */
  98.  
  99.     strcpy (ab, "abcdef");
  100.     printf ("\n strcpy (ab, \"abcdefg\") \n ab = \"%s\"\n", ab);
  101.  
  102.  
  103. /* Concat */
  104.  
  105.     printf ("\n* Concat \n");
  106.  
  107.   /* char *strcat(char *dest, const char *src); */
  108.  
  109.     strcat (ab, aa);
  110.     printf ("\n strcat (ab, aa) \n ab = \"%s\"\n", ab);
  111.  
  112.   /* char *strncat(char *dest, const char *src, size_t n); */
  113.  
  114.     strncat (ab, aa, 3);
  115.     printf ("\n strncat (ab, aa, 3) \n ab = \"%s\"\n", ab);
  116.  
  117.  
  118. /* Compare */
  119.  
  120.     printf ("\n* Compare \n");
  121.  
  122.   /* int memcmp(const void *s1, const void *s2, size_t n); */
  123.  
  124.     x = memcmp("b", "abc", 1);
  125.     printf ("\n x = memcmp(\"b\", \"abc\", 1) \n x = %d \n", x);
  126.     
  127.     x = memcmp("b", "bcd", 1);
  128.     printf ("\n x = memcmp(\"b\", \"bcd\", 1) \n x = %d \n", x);
  129.     
  130.     x = memcmp("b", "cde", 1);
  131.     printf ("\n x = memcmp(\"b\", \"cde\", 1) \n x = %d \n", x);
  132.     
  133.     x = memcmp("b", "b\0cd", 100);
  134.     printf ("\n x = memcmp(\"b\", \"b\\0cd\", 100) \n x = %d \n", x);
  135.     
  136.   /* int strncmp(const char *s1, const char *s2, size_t n); */
  137.  
  138.     x = strncmp("b", "abc", 1);
  139.     printf ("\n\n x = strncmp(\"b\", \"abc\", 1) \n x = %d \n", x);
  140.     
  141.     x = strncmp("b", "bcd", 1);
  142.     printf ("\n x = strncmp(\"b\", \"bcd\", 1) \n x = %d \n", x);
  143.     
  144.     x = strncmp("b", "cde", 1);
  145.     printf ("\n x = strncmp(\"b\", \"cde\", 1) \n x = %d \n", x);
  146.     
  147.     x = strncmp("b", "b\0cd", 100);
  148.     printf ("\n x = strncmp(\"b\", \"b\\0cd\", 100) \n x = %d \n", x);
  149.     
  150.   /* int strcmp(const char *s1, const char *s2); */
  151.  
  152.     x = strcmp("b", "abc");
  153.     printf ("\n\n x = strcmp(\"b\", \"abc\") \n x = %d \n", x);
  154.     
  155.     x = strcmp("b", "a");
  156.     printf ("\n x = strcmp(\"b\", \"a\") \n x = %d \n", x);
  157.     
  158.     x = strcmp("b", "bcd");
  159.     printf ("\n x = strcmp(\"b\", \"bcd\") \n x = %d \n", x);
  160.     
  161.     x = strcmp("b", "b");
  162.     printf ("\n x = strcmp(\"b\", \"b\") \n x = %d \n", x);
  163.     
  164.     x = strcmp("b", "cde");
  165.     printf ("\n x = strcmp(\"b\", \"cde\") \n x = %d \n", x);
  166.     
  167.     x = strcmp("b", "c");
  168.     printf ("\n x = strcmp(\"b\", \"c\") \n x = %d \n", x);
  169.     
  170.     x = strcmp("b", "b\0cd");
  171.     printf ("\n x = strcmp(\"b\", \"b\\0cd\") \n x = %d \n", x);
  172.     
  173.   /* int strcoll(const char *s1, const char *s2); */
  174.  
  175.     x = strcoll("b", "a");
  176.     printf ("\n\n x = strcoll(\"b\", \"abc\") \n x = %d \n", x);
  177.     
  178.     x = strcoll("b", "b");
  179.     printf ("\n x = strcoll(\"b\", \"b\") \n x = %d \n", x);
  180.     
  181.     x = strcoll("b", "c");
  182.     printf ("\n x = strcoll(\"b\", \"cde\") \n x = %d \n", x);
  183.     
  184.     x = strcoll("b", "b\0cd");
  185.     printf ("\n x = strcoll(\"b\", \"b\\0cd\") \n x = %d \n", x);
  186.     
  187.   /* size_t strxfrm(const char *s1, const char *s2, size_t n); */
  188.  
  189.     strncpy (ac, "abcdefghijklmnopqrstuvwxyz", 27);
  190.     strncpy (aa, "efghijklmonp", 13);
  191.  
  192.     printf ("\n ac = %s \n", ac);
  193.     printf ("\n aa = %s \n", aa);
  194.     
  195.     y = strxfrm(ac, aa, 27);
  196.     printf ("\n y = strxfrm(ac, aa, 27) \n y = %d \n ac = \"%s\"\n", y, ac);
  197.  
  198.  
  199. /* Search */
  200.  
  201.     printf ("\n* Search \n");
  202.  
  203.   /* void *memchr(const void *s, int c, size_t n); */
  204.  
  205.     ba = memchr (ab, 'd', 10);
  206.     printf ("\n ba = memchr(%s, 'd', 10) \n ba = \"%s\" \n ", ab, ba);
  207.  
  208.   /* size_t *strcspn(const char *s, const char *reject); */
  209.  
  210.     y = strcspn (ab, "dgh");
  211.     printf ("\n strcspn (\"%s\", \"dgh\")  \n y = %d \n ab[%d] = %s \n", ab, y, y, &ab[y]);
  212.     
  213.   /* size_t *strspn(const char *s, const char *accept); */
  214.  
  215.     y = strspn (ab, "abcdef");
  216.     printf ("\n strspn (\"%s\", \"abcdef\")  \n y = %d \n ab[%d] = %s \n", ab, y, y, &ab[y]);
  217.     
  218.   /* char *strpbrk(const char *s, const char *accept); */
  219.  
  220.     ba = strpbrk(ab, "cde");
  221.     printf ("\n strpspn (\"%s\", \"cde\")  \n ba = %s \n", ab, ba);
  222.  
  223.   /* char *strchr(const char *s, int c); */
  224.  
  225.     ba = strchr (ab, 'h');
  226.     printf ("\n ba = strchr(%s, 'h') \n ba = \"%s\" \n ", ab, ba);
  227.  
  228.   /* char *strrchr(const char *s, int c); */
  229.  
  230.     ba = strrchr (ab, 'h');
  231.     printf ("\n ba = strrchr(%s, 'h') \n ba = \"%s\" \n ", ab, ba);
  232.  
  233.   /* char *strstr(const char *s, const char *substring); */
  234.  
  235.     ba = strstr (ab, "hij");
  236.     printf ("\n ba = strchr(%s, 'hij') \n ba = \"%s\" \n ", ab, ba);
  237.  
  238.     ba = strstr (ab, "ghig");
  239.     printf ("\n ba = strchr(%s, 'ghij') \n ba = \"%s\" \n ", ab, ba);
  240.  
  241.   /* char *strtok(char *s, const char *delim); */
  242.  
  243.     strcpy(ad, "This is an example sentence.");
  244.     printf("\n strcpy(ad, \"This is an example sentence.\") \n"); 
  245.  
  246.     bb = strtok(ad, " ");
  247.     while (bb) {
  248.         printf ("  bb = \"%s\"\n", bb);
  249.         bb = strtok(NULL, " ");
  250.     }
  251.         
  252.  
  253. /* Misc */
  254.  
  255.     printf ("\n* Misc \n");
  256.  
  257.   /* void *memset(void *s, int c, size_t n); */
  258.  
  259.     memset(ac, 'a', 1000);
  260.     ac[999] = '\0';
  261.     printf ("\n memset(ac, 'a', 1000); \n ac[999] = '\\0'; \n ac = \"%s\" \n", ac);
  262.  
  263.   /* char *strerror(int errnum); */
  264.  
  265.     printf ("\n strerror(5) = %s \n", strerror (5));
  266.  
  267.   /* size_t *strlen(const char *s); */
  268.  
  269.     y = strlen(ac);
  270.     printf("\n y = strlen(ac); \n y = %d \n", y);
  271.  
  272. /* Non Portable */
  273.  
  274.     printf ("\n* Non Portable \n");
  275.  
  276.   /* int strcasecmp(const char *s1, const char *s2); */
  277.  
  278.     x = strcasecmp("b", "ABC");
  279.     printf ("\n\n x = strcasecmp(\"b\", \"ABC\") \n x = %d \n", x);
  280.  
  281.     x = strcasecmp("b", "A");
  282.     printf ("\n x = strcasecmp(\"b\", \"A\") \n x = %d \n", x);
  283.  
  284.     x = strcasecmp("b", "BCD");
  285.     printf ("\n x = strcasecmp(\"b\", \"BCD\") \n x = %d \n", x);
  286.  
  287.     x = strcasecmp("b", "B");
  288.     printf ("\n x = strcasecmp(\"b\", \"B\") \n x = %d \n", x);
  289.  
  290.     x = strcasecmp("b", "CDE");
  291.     printf ("\n x = strcasecmp(\"b\", \"CDE\") \n x = %d \n", x);
  292.  
  293.     x = strcasecmp("b", "C");
  294.     printf ("\n x = strcasecmp(\"b\", \"C\") \n x = %d \n", x);
  295.  
  296.     x = strcasecmp("b", "B\0CD");
  297.     printf ("\n x = strcasecmp(\"b\", \"B\\0CD\") \n x = %d \n", x);
  298.  
  299.   /* int strncasecmp(const char *s1, const char *s2, size_t n); */
  300.  
  301.     x = strncasecmp("b", "ABC", 1);
  302.     printf ("\n\n x = strncasecmp(\"b\", \"ABC\", 1) \n x = %d \n", x);
  303.  
  304.     x = strncasecmp("b", "BCD", 1);
  305.     printf ("\n x = strncasecmp(\"b\", \"BCD\", 1) \n x = %d \n", x);
  306.  
  307.     x = strncasecmp("b", "CDE", 1);
  308.     printf ("\n x = strncasecmp(\"b\", \"CDE\", 1) \n x = %d \n", x);
  309.  
  310.     x = strncasecmp("b", "B\0CD", 100);
  311.     printf ("\n x = strncasecmp(\"b\", \"B\\0CD\", 100) \n x = %d \n", x);
  312.  
  313.   /* char *strdup(const char *s); */
  314.  
  315.     ba = strdup ("Test of strdup");
  316.     printf("\n ba = strdup (\"Test of strdup\"); \n ba = %s \n", ba);
  317.  
  318.   /* char *strfry(char *string); */
  319.  
  320.     strfry(ba);
  321.     printf("\n strfry (ba); \n ba = %s \n", ba);
  322.     free (ba);
  323.  
  324.   /* char *strsep(char **stringp, const char *delim); */
  325.  
  326.     strcpy(ad, "This is a second example sentence.");
  327.     printf("\n strcpy(ad, \"%s\") \n", ad); 
  328.  
  329.     ba = ad;
  330.  
  331.     bb = strsep(&ba, " ");
  332.     while (bb) {
  333.         printf ("  bb = \"%s\"\n", bb);
  334.         bb = strsep(&ba, " ");
  335.     }
  336.     
  337.     printf ("\n ad == %s\n", ad);
  338.     printf ("\n");
  339.  
  340.   /* char *index(const char *s, int c); */
  341.   /* use strchr instead, it is the same function, only portable */
  342.  
  343.   /* char *rindex(const char *s, int c); */
  344.   /* use strrchr instead, it is the same function, only portable */
  345.  
  346. }
  347.